﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Shell32;  //用ShellClass来获取文件属性
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace MusicPlayer
{
    //歌曲导入程序后，我们显然需获取并储存每首歌曲的各种信息，以便之后需要能够直接读取，
    //所以在这里新一个SongsInfo类来存储歌曲的各种信息（歌曲名、路径、歌手等等）
    class SongsInfo
    {
        private string filePath;
        private string filesize;    //1---文件大小
        private string artist;      //13--参与创作的艺术家
        private string album;       //14--唱片集
        private string originName;  //21--标题
        private string duration;     //27--时长
        private Image albumImage;
        private Image smallAblum;

        public string FilePath { get => filePath; set => filePath = value; }
        public string Filesize { get => filesize; set => filesize = value; }
        public string Artist { get => artist; set => artist = value; }
        public string Album { get => album; set => album = value; }
        public string OriginName { get => originName; set => originName = value; }
        public string Duration { get => duration; set => duration = value; }
        public Image AlbumImage { get => albumImage; set => albumImage = value; }
        public Image SmallAblum { get => smallAblum; set => smallAblum = value; }

        public SongsInfo(string fPath)
        {
            SetSongInfo(fPath);
            SetAlbumArt(fPath);
        }

        public void SetSongInfo(string strPath)
        {
            try
            {
                //初始化Shell接口
                Shell32.ShellClass sh = new Shell32.ShellClass();
                //获取文件所在父目录对象
                Folder dir = sh.NameSpace(Path.GetDirectoryName(strPath));
                //获取文件对象的FolderItem对象
                FolderItem item = dir.ParseName(Path.GetFileName(strPath));

                FilePath = strPath;
                filesize = dir.GetDetailsOf(item, 1);
                if (filesize == string.Empty)
                    filesize = "未知";

                artist = dir.GetDetailsOf(item, 13);
                if (artist == string.Empty)
                    artist = "未知";

                album = dir.GetDetailsOf(item, 14);
                if (album == string.Empty)
                    album = "未知";

                OriginName = dir.GetDetailsOf(item, 21);
                if (OriginName == string.Empty)
                    OriginName = "未知";

                duration = dir.GetDetailsOf(item, 27);
                if (duration == string.Empty)
                    duration = "未知";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void SetAlbumArt(string strPath)
        {
            if (strPath != "" && strPath != null)
            {
                //将得到的文件实例化
                TagLib.File file = TagLib.File.Create(strPath);

                //歌曲属性均在实例文件的tag属性属性中
                //不一定每首歌都有专辑图片，需要判断
                if (file.Tag.Pictures.Length > 0)
                {
                    //tag中的图片信息为byte数组，需要用函数进行转化
                    byte[] bin = file.Tag.Pictures[0].Data.Data;
                    albumImage = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(900, 900, null, IntPtr.Zero);
                    /*public Image GetThumbnailImage(
                       int thumbWidth,
                       int thumbHeight,
                       Image.GetThumbnailImageAbort callback,
                       IntPtr callbackData
                       );
                      请求的缩略图的宽度和高度（以像素为单位） callbackData必须是 IntPtr.Zero
                     * */
                    albumImage = Cut((Bitmap)albumImage, 31, 155, 837, 537);
                    smallAblum = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(66, 66, null, IntPtr.Zero);
                    return;
                }
                albumImage = MusicPlayer.Properties.Resources.defaultBigAblum;
                smallAblum = MusicPlayer.Properties.Resources.defaultSmallAblum;
            }
        }

        //Bitmap是用于处理由像素数据定义的图像的对象
        public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }
            int w = b.Width;
            int h = b.Height;
            if (StartX >= w || StartY >= h)
            {
                return null;
            }
            if (StartX + iWidth > w)
            {
                iWidth = w - StartX;
            }
            if (StartY + iHeight > h)
            {
                iHeight = h - StartY;
            }
            try
            {
                //PixelFormat: (指定图像中每个像素的颜色数据的格式)
                Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
                Graphics g = Graphics.FromImage(bmpOut);
                //在指定位置绘制Image对象指定部分
                //GraphicsUnit units = GraphicsUnit.Pixel;//源矩形的度量单位设置为像素
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
    }
}
